package Dao;

import model.*;
import org.apache.commons.dbutils.*;
import utils.*;
import java.math.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.dbutils.handlers.*;

public class OrderDao {
    public void insertOrder(Connection con, Order order) throws SQLException {
        QueryRunner r = new QueryRunner();
        String sql = "insert into `orders`(code,product_id,buy_price,amount,user_id,username,seller_id) values(?,?,?,?,?,?,?)";
        r.update(con,sql,
                order.getCode(),order.getProduct_id(),order.getBuy_price(),
                order.getAmount(),order.getUser().getId(),order.getUser().getUsername(),order.getSeller_id());
    }
    public int getLastInsertId(Connection con) throws SQLException {//获取最近一次执行过插入操作的订单的订单id
        QueryRunner r = new QueryRunner();
        String sql = "select last_insert_id()";
        BigInteger bi = r.query(con, sql,new ScalarHandler<BigInteger>());
        return Integer.parseInt(bi.toString());
    }
    public void insertOrderItem(Connection con, OrderItem item) throws SQLException {
        QueryRunner r = new QueryRunner();
        String sql ="insert into orderitem(price,amount,product_id,order_id) values(?,?,?,?)";
        r.update(con,sql,item.getPrice(),item.getAmount(),item.getGoods().getId(),item.getOrder().getId());
    }
    //根据买家id获取order list
    public List<Order> selectAll(int userid) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from `orders` where user_id=?";
        return r.query(sql, new BeanListHandler<Order>(Order.class),userid);
    }
    //根据卖家id获取order list
    public List<Order> selectAllBySellerId(int seller_id) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from `orders` where seller_id=?";
        return r.query(sql, new BeanListHandler<Order>(Order.class),seller_id);
    }
    public List<Order> GetAllOrder() throws SQLException {
        QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from orders";
        //        try {
        //            Connection conn = DataSourceUtils.getDataSource().getConnection();
        //            System.out.println("conn对象：" + conn);
        //            System.out.println("当前连接到的数据库是：" + conn.getCatalog());
        //        } catch (Exception e) {
        //            e.printStackTrace();
        //            System.out.println("获取数据库连接时发生异常！");
        //        }
        return r.query(sql,new BeanListHandler<Order>(Order.class));
    }
    public List<OrderItem> selectAllItem(int orderid) throws SQLException{
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select i.id,i.price,i.amount,p.name from orderitem i,product p where order_id=? and i.product_id=p.id";
        return r.query(sql, new BeanListHandler<OrderItem>(OrderItem.class),orderid);
    }
//    public int getOrderCount(int status) throws SQLException {//根据订单状态(0购入，1售出)获取该状态的订单数
//        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
//        String sql = "select count(*) from `orders` where status=?";
//        return r.query(sql, new ScalarHandler<Long>(),status).intValue();
//    }
//    public List<Order> selectOrderList(int status) throws SQLException {//通过订单状态获取订单列表
//        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
//        String sql = "select o.id,o.code,o.product_id,o.buy_price,o.amount,o.user_id,o.status,u.username from `orders` o,user u where o.user_id=u.id and o.status=?";
//        return r.query(sql, new BeanListHandler<Order>(Order.class),status);
//    }
    public void deleteOrder(Connection con ,int id) throws SQLException {
        QueryRunner r = new QueryRunner();
        String sql ="delete from `orders` where id = ?";
        r.update(con,sql,id);
    }
    public void deleteOrderItem(Connection con ,int id) throws SQLException {
        QueryRunner r = new QueryRunner();
        String sql ="delete from orderitem where order_id=?";
        r.update(con,sql,id);
    }
}
